package nl.infodation.emr.pricingservice.security.login;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import nl.infodation.emr.pricingservice.entity.User;
import nl.infodation.emr.utility.Constants;
import nl.infodation.emr.utility.EncryptionUtil;
import nl.infodation.emr.utility.EntityManagerFactoryUtil;
import nl.infodation.emr.utility.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.peholmst.i18n4vaadin.I18N;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class LoginController{

	private static final Logger LOG = LoggerFactory.getLogger(LoginController.class);
	private static final EntityManagerFactory EM_FACTORY = 
			EntityManagerFactoryUtil.getEntityManagerFactory(Constants.PRICING_PERSISTENCE_UNIT);

	/**
	 * Authorize account login
	 * @param txtUsername
	 * @param txtPassword
	 * @return UserProfile
	 */
	public User checkLogin(final TextField txtUsername, final PasswordField txtPassword, I18N i18n) {
		
		EntityManager entityManager = null;
		User result = new User();
		
		final String userName = txtUsername.getValue().toString();
		final String passWord = txtPassword.getValue().toString();
		
		txtUsername.setComponentError(null);
		txtPassword.setComponentError(null);
		
		if (StringUtil.isEmptyOrBlank(userName)){
			setComponentError(txtUsername, "Incorrect UserName");
		}else if (StringUtil.isEmptyOrBlank(passWord)) {
			setComponentError(txtPassword, "Incorrect PassWord");			
		}else {			
			try {
				entityManager = EM_FACTORY.createEntityManager();
				result = entityManager.find(User.class, userName);
				if(result == null) {
					setComponentError(txtUsername,i18n.getMessage(Constants.USER_INCORRECT));
					LOG.info("check again your username");
					return null;
					
				}else if(!EncryptionUtil.encryption(passWord).equalsIgnoreCase(result.getPassword())) {
					setComponentError(txtPassword, i18n.getMessage(Constants.PASSWORD_INCORRECT));
					LOG.info("check again your password");
					return null;
				}else if (!result.isActive()) {
					setComponentError(txtUsername, i18n.getMessage(Constants.USER_ACTIVE));
					LOG.info("User not active");
					return null;
				}				
			} catch (IllegalStateException illegalStateException) {
				LOG.error("Error reading user profiles", illegalStateException);
				return new User();
			} catch (IllegalArgumentException illegalArgumentException) {
				LOG.error("Error reading user profiles", illegalArgumentException);
				if (entityManager != null && entityManager.getTransaction().isActive()) {
					entityManager.getTransaction().rollback();
				}
				return new User();
			} finally {
				if (entityManager != null && entityManager.isOpen()) {
					entityManager.close();
				}
			}
		}
		return result;
	}
	
	private void setComponentError(final AbstractTextField component, final String msgError) {
		component.focus();
		component.setComponentError(new UserError(msgError));
	}
}
